"""
Case Type   : GUC参数
Case Name   : 验证参数undo_limit_size_per_transaction的异常值
Create At   : 2022-01-21
Owner       : guoweibiao
Description :
    1.查询undo_space_limit_size参数默认值
    2.查询undo_limit_size_per_transaction参数默认值
    3.修改undo_limit_size_per_transaction参数为异常值
    4.show参数
    5.清理环境
Expect      :
    1.参数默认值256GB
    2.参数默认值32GB
    3.修改参数默认值失败
    4.参数值为默认值
    5.恢复参数默认值成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Guc(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.default_usls_param = '256GB'
        self.default_ulspt_param = '32GB'

    def test_search_path(self):
        text = '-----step1:查询undo_space_limit_size参数默认值; ' \
               'expect:参数默认值256GB-----'
        self.log.info(text)
        res = self.common.show_param('undo_space_limit_size')
        self.log.info(res)
        self.assertIn(self.default_usls_param, res, '执行失败：' + text)

        text = '-----step2:查询undo_limit_size_per_transaction参数默认值; ' \
               'expect:参数默认值32GB-----'
        self.log.info(text)
        res = self.common.show_param('undo_limit_size_per_transaction')
        self.log.info(res)
        self.assertIn(self.default_ulspt_param, res, '执行失败：' + text)

        text = '-----步骤3:修改undo_limit_size_per_transaction参数为异常值；' \
               'expect:执行失败-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'reload', self.constant.GSGUC_SUCCESS_MSG,
            "undo_limit_size_per_transaction = 'abc'")
        self.log.info(res)
        self.assertFalse(res)

        text = '-----步骤4:show参数;expect:参数值为默认值-------'
        self.log.info(text)
        res = self.common.show_param('undo_limit_size_per_transaction')
        self.log.info(res)
        self.assertIn(self.default_ulspt_param, res, '执行失败：' + text)

    def tearDown(self):
        self.log.info('-----步骤5:恢复默认值;expect:成功-----')
        curr_param = self.common.show_param('undo_limit_size_per_transaction')
        if curr_param != self.default_ulspt_param:
            res = self.pri_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG,
                f'undo_limit_size_per_transaction={self.default_ulspt_param}')
            self.log.info(res)
            self.assertTrue(res)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
